var upload = require('../../utils/upload_file.js')
var base = require('../../utils/base.js')
var memberId = base.getMemberData().memberId
var page = 1
//获取应用实例
var app = getApp()
Page({
  data: {
    login: false,
    height: 0,
    clockData: []
  },
  onLoad: function () {

    var that = this
    this.init() //登录


    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight
        })
      }
    })

  },
  init: function () {
    var that = this
    app.getUserInfo(function (userInfo, status) {
      if (status) {//授权成功
        memberId = base.getMemberData().memberId
        that.setData({
          login: true
        })
        that.loadData()
        console.log('授权成功')
      } else {//授权失败
        console.log('授权失败')
        that.openSetting()
      }
    }, false)
  },
  loadData: function (status) {
    var that = this
    if (!status) {
      page = 1
    }
    //获取打卡信息
    base.getJson('JamyoClock/jamyoClockLists', { row: page * 10 }, function (data) {
      console.log(data)
      for (var i = 0; i < data.clocks.length; i++) {
        // if (data.clocks[i].jamyo_clock_video.video != null) {
        //   data.clocks[i].text = '视频'
        // } else {
        //   data.clocks[i].text = '照片'
        // }
        data.clocks[i].created_at = base.timeago(data.clocks[i].created_at)
      }
      that.setData({
        clockData: data
      })
      page += 1
    })

  },
  //加载更多
  onReachBottom: function () {
    this.loadData(1)
  },
  openSetting: function () {
    var that = this
    wx.openSetting({
      success: (res) => {
        console.log(res)
        if (res.authSetting['scope.userInfo']) {
          that.init()
        }
      }
    })
  },
  chooseMedia: function () {
    var that = this
    wx.showActionSheet({
      itemList: ['上传30秒视频'],
      success: function (res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0) {
          wx.chooseVideo({
            sourceType: ['album'],
            maxDuration: 60,
            camera: 'back',
            success: function (res) {
              console.log(res)
              if (res.duration < 31) {
                base.showLoading('上传中，请勿关闭小程序')
                //上传图片
                upload.qiniuUpload(res.tempFilePath, function (res) {
                  wx.hideLoading()
                  console.log('https://oos.jamyo.net/' + JSON.parse(res).key)
                  that.clock(null, 'https://oos.jamyo.net/' + JSON.parse(res).key)

                })
              } else {
                wx.showToast({
                  title: '视频不能大于30秒',
                  image: '../../image/warning.png',
                  duration: 1500
                })

              }
            }
          })
        }
      }
    })
  },
  clock: function (gallery, video, pic) {
    var post_data = { gallery: gallery, video: video, pic: pic }
    console.log(post_data)
    var that = this
    base.getJson('JamyoClock/jamyoClock', post_data, function (data) {
      console.log(data)
      that.loadData()
    })
  },
   //设置分享
  onShareAppMessage: function () {
    return {
      title: '一起来JAMYO舞蹈打卡',
      desc: '你也一起来JAMYO舞蹈小程序打卡吗？',
      path: '/pages/index/index'
    }
  }
})
